/**
 * Controller that handles events in the list view
 * 
 * @version 1.2
 * @author Tyler Bishop
 */

package com.BlackMesa.controllers;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TimePicker;

import com.BlackMesa.database.DatabaseAdapter;
import com.BlackMesa.models.*;
import com.google.android.maps.GeoPoint;

public class ListViewController implements OnClickListener, OnItemSelectedListener {
	private Activity activity;
	private User currentUser;
	private TableLayout tableView;
	private Button addTask, createTaskButton, cancelButton, useDate, useTime,
			cancelDate, cancelTime, doneButton, editTaskButton;
	private ImageButton colorButton1,colorButton2,locButton1,locButton2;
	private EditText dateField, timeField, viewdateField, viewtimeField, viewTaskName, viewLocation,
	viewDescription, viewCategory, locField;
	private ImageView allTasks;
	private Spinner dropDown;
	private Dialog newTaskDialog, pickDateDialog, pickTimeDialog, viewTaskDialog,pickColorDialog,pickLocationDialog;
	private DatePicker datePicker;
	private TimePicker timePicker;
	private ArrayAdapter<String> adapter;
	private ArrayList<String> categories;
	private ArrayList<Task> tasks;
	private Map<ImageView,Integer> colorButtons;
	private Map<Integer,Integer> buttonToColor;
	private HashMap<CheckBox,Task> taskMap;
	private Task taskBeingViewed;
	private DatabaseAdapter adapt;
	private boolean inEdit = false;
	private OurLocation currLocation;
	private int currentColor;

	/**
	 * initializes the user and activity
	 * 
	 * @param user
	 *            current user that is logged in
	 * @param activity
	 *            passes in the current activity
	 */
	public ListViewController(User user, Activity activity, DatabaseAdapter adapter) {
		this(activity);
		adapt = adapter;
		currentUser = user;
	}
	public ListViewController(Activity activity) {
		this.activity = activity;
	}
	
	public void init() {
		addTask = (Button) activity.findViewById(R.id.addTask);
		allTasks = (ImageView) activity.findViewById(R.id.allTasks);
		dropDown = (Spinner) activity.findViewById(R.id.dropDown);
		tableView = (TableLayout) activity.findViewById(R.id.tableView);
		categories = new ArrayList<String>();
		taskMap = new HashMap<CheckBox, Task>();
		tasks = new ArrayList<Task>();
		colorButtons = new HashMap<ImageView,Integer>();
		buttonToColor = new HashMap<Integer,Integer>();
		categories.add("All Tasks");
		//categories.add(currentUser.getCategories().get(0).getName());
		categories.add("After date...");
		categories.add("Incomplete");
		for (int i = 1; i < currentUser.getCategories().size(); i++) {
			categories.add(currentUser.getCategories().get(i).getName());
		}
		currentUser.setActiveCategory(currentUser.getCategories().get(0));
		adapter = new ArrayAdapter<String>(activity,
				android.R.layout.simple_dropdown_item_1line, categories);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		dropDown.setAdapter(adapter);
		addTask.setOnClickListener(this);
		allTasks.setOnClickListener(this);
		dropDown.setOnItemSelectedListener(this);
	}
	
	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}
	public User getCurrentUser() {
		return currentUser;
	}

	/**
	 * method to instantiate a dialog box that allows the user to create a task
	 */
	private void newTaskDialog() {
		Dialog dialog = new Dialog(activity);
		dialog.setContentView(R.layout.new_task);
		dialog.setTitle("Create new task");
		dialog.show();
		this.newTaskDialog = dialog;
		createTaskButton = (Button) dialog.findViewById(R.id.createTask);
		cancelButton = (Button) dialog.findViewById(R.id.cancelTask);
		dateField = (EditText) dialog.findViewById(R.id.dateField);
		timeField = (EditText) dialog.findViewById(R.id.timeField);
		locButton1 = (ImageButton) dialog.findViewById(R.id.locButton1);
		colorButton1 = (ImageButton) dialog.findViewById(R.id.catColor1);
		locField = (EditText)dialog.findViewById(R.id.locationField);
		locButton1.setOnClickListener(new NewTask());
		colorButton1.setOnClickListener(new NewTask());
		dateField.setOnClickListener(new NewTask());
		timeField.setOnClickListener(new NewTask());
		createTaskButton.setOnClickListener(new NewTask());
		cancelButton.setOnClickListener(new NewTask());
	}

	/**
	 * instantiates a dialog that allows user to pick a date for a task
	 */
	private void pickDateDialog(OurListener ocl) {
		Dialog dialog = new Dialog(activity);
		dialog.setContentView(R.layout.date_chooser);
		dialog.setTitle("Pick a date");
		dialog.show();
		datePicker = (DatePicker) dialog.findViewById(R.id.datePicker);
		this.pickDateDialog = dialog;
		useDate = (Button) dialog.findViewById(R.id.useDate);
		cancelDate = (Button) dialog.findViewById(R.id.cancelDate);
		useDate.setOnClickListener(ocl.newDate());
		cancelDate.setOnClickListener(ocl.newDate());
	}

	/**
	 * instantiates a dialog that allows the user to pick a time for a task
	 */
	private void pickTimeDialog(OurListener ocl) {
		Dialog dialog = new Dialog(activity);
		dialog.setContentView(R.layout.time_chooser);
		dialog.setTitle("Pick a time");
		dialog.show();
		timePicker = (TimePicker) dialog.findViewById(R.id.timePicker);
		this.pickTimeDialog = dialog;
		useTime = (Button) dialog.findViewById(R.id.useTime);
		cancelTime = (Button) dialog.findViewById(R.id.cancelTime);
		useTime.setOnClickListener(ocl.newTime());
		cancelTime.setOnClickListener(ocl.newTime());
	}
	
	private void pickColorDialog(OurListener ocl) {
		Dialog dialog = new Dialog(activity);
		dialog.setContentView(R.layout.color_picker);
		dialog.setTitle("Pick a color");
		dialog.show();
		this.pickColorDialog = dialog;
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView1),R.drawable.darkred);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView2),R.drawable.medred);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView3),R.drawable.lightred);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView4),R.drawable.darkorange);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView5),R.drawable.medorange);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView6),R.drawable.lightorange);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView7),R.drawable.darkgreen);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView8),R.drawable.medgreen);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView9),R.drawable.lightgreen);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView10),R.drawable.darkblue);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView11),R.drawable.medblue);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView12),R.drawable.lightblue);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView13),R.drawable.darkpurple);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView14),R.drawable.medpurple);
		colorButtons.put((ImageView)dialog.findViewById(R.id.imageView15),R.drawable.lightpurple);
		buttonToColor.put(R.drawable.darkred,Color.rgb(114,9,9));
		buttonToColor.put(R.drawable.medred,Color.rgb(229,31,31));
		buttonToColor.put(R.drawable.lightred,Color.rgb(237,110,110));
		buttonToColor.put(R.drawable.darkorange,Color.rgb(196,106,8));
		buttonToColor.put(R.drawable.medorange,Color.rgb(247,139,30));
		buttonToColor.put(R.drawable.lightorange,Color.rgb(249,170,98));
		buttonToColor.put(R.drawable.darkgreen,Color.rgb(4,84,4));
		buttonToColor.put(R.drawable.medgreen,Color.rgb(5,193,5));
		buttonToColor.put(R.drawable.lightgreen,Color.rgb(115,239,115));
		buttonToColor.put(R.drawable.darkblue,Color.rgb(7,38,168));
		buttonToColor.put(R.drawable.medblue,Color.rgb(7,62,237));
		buttonToColor.put(R.drawable.lightblue,Color.rgb(78,125,242));
		buttonToColor.put(R.drawable.darkpurple,Color.rgb(129,10,142));
		buttonToColor.put(R.drawable.medpurple,Color.rgb(199,12,226));
		buttonToColor.put(R.drawable.lightpurple,Color.rgb(226,136,242));
		for(ImageView b : colorButtons.keySet()) {
			b.setOnClickListener(ocl.newColor());
		}
	}
	
	/**
	 * instantiates a dialog that allows the user to view a task's details
	 */
	private void viewTaskDialog() {
		Dialog dialog = new Dialog(activity);
		dialog.setContentView(R.layout.view_task);
		dialog.setTitle(taskBeingViewed.getName());
		dialog.show();
		this.viewTaskDialog = dialog;
		editTaskButton = (Button) dialog.findViewById(R.id.editTask);
		doneButton = (Button) dialog.findViewById(R.id.doneTask);
		viewdateField = (EditText) dialog.findViewById(R.id.viewDateField);
		viewtimeField = (EditText) dialog.findViewById(R.id.viewTimeField);
		if(taskBeingViewed.getDueDate() != null) {
			viewdateField.setText(new SimpleDateFormat("MM/dd/yyyy")
			.format(taskBeingViewed.getDueDate()));
		}
		if(taskBeingViewed.getDueTime() != null) {
			viewtimeField.setText(new SimpleDateFormat("hh:mm")
			.format(taskBeingViewed.getDueTime()));
		}
		viewTaskName = (EditText) dialog.findViewById(R.id.viewNameField);
		viewTaskName.setText(taskBeingViewed.getName());
		viewLocation = (EditText) dialog.findViewById(R.id.locField);
		viewLocation.setText(taskBeingViewed.getLocation().getName());
		viewDescription = (EditText) dialog.findViewById(R.id.viewDescriptionField);
		viewDescription = (EditText) dialog
				.findViewById(R.id.viewDescriptionField);
		viewDescription.setText(taskBeingViewed.getDetailedDescription());
		viewCategory = (EditText) dialog.findViewById(R.id.categoryField);
		viewCategory.setText(taskBeingViewed.getCategory().getName());
		locButton2 = (ImageButton) dialog.findViewById(R.id.locButton2);
		colorButton2 = (ImageButton) dialog.findViewById(R.id.catColor2);
		viewTaskName.setEnabled(false);
		viewLocation.setEnabled(false);
		viewDescription.setEnabled(false);
		viewCategory.setEnabled(false);
		locButton2.setEnabled(false);
		colorButton2.setEnabled(false);
		viewdateField.setEnabled(false);
		viewtimeField.setEnabled(false);
		editTaskButton.setOnClickListener(new ViewTask());
		doneButton.setOnClickListener(new ViewTask());
	}

	/**
	 * provides the capability for a user to edit a task
	 */
	private void editTask() {
		viewdateField.setOnClickListener(new ViewTask());
		viewtimeField.setOnClickListener(new ViewTask());
		locButton2.setOnClickListener(new ViewTask());
		colorButton2.setOnClickListener(new ViewTask());
		locButton2.setEnabled(true);
		colorButton2.setEnabled(true);
		viewTaskName.setEnabled(true);
		viewLocation.setEnabled(true);
		viewDescription.setEnabled(true);
		viewCategory.setEnabled(true);
		viewdateField.setEnabled(true);
		viewtimeField.setEnabled(true);
		editTaskButton.setText("Save");
		doneButton.setText("Cancel");
	}

	/**
	 * disables the editability of a task
	 */
	private void viewTask() {
		viewdateField.setOnClickListener(null);
		viewtimeField.setOnClickListener(null);
		viewTaskName.setEnabled(false);
		viewLocation.setEnabled(false);
		viewDescription.setEnabled(false);
		viewCategory.setEnabled(false);
		viewdateField.setEnabled(false);
		viewtimeField.setEnabled(false);
		editTaskButton.setText("Edit");
		doneButton.setText("Done");
	}

	/**
	 * resets a task to its original contents upon clicking cancel
	 */
	private void cancelEditTask() {// TODO: date and time text
		viewCategory.setText(taskBeingViewed.getCategory().getName());
		viewDescription.setText(taskBeingViewed.getDetailedDescription());
		viewLocation.setText(taskBeingViewed.getLocation().getName());
		viewTaskName.setText(taskBeingViewed.getName());
		viewTask();
	}

	/**
	 * updates the task to what the user edited to
	 */
	private void saveCurrentTask(){//TODO: category, date, and time text
		Category cat;
		taskBeingViewed.setName(viewTaskName.getText().toString());
		taskBeingViewed.setDetailedDescription(viewDescription.getText().toString());
		currentUser.setLocations(locationSender.getLoc());
		if(locationSender.lastPick() != null) {
			taskBeingViewed.setLocation(new OurLocation(viewLocation.getText().toString(),locationSender.lastPick()));
		}
		locationSender.setNewLast(null);
		cat = currentUser.createCategory(viewCategory.getText().toString(), 0, taskBeingViewed);
		taskBeingViewed.setDetailedDescription(viewDescription.getText()
				.toString());
		adapt.editTask(taskBeingViewed);
		adapt.editCategory(cat);
	}

	/**
	 * creates an alert box and sets it focus to current
	 * 
	 * @param title
	 *            the header of the alertbox
	 * @param mymessage
	 *            the message displayed in the alertbox
	 */
	public void alertBox(String title, String mymessage) {
		new AlertDialog.Builder(this.activity)
				.setMessage(mymessage)
				.setTitle(title)
				.setCancelable(true)
				.setNeutralButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
							}
						}).show();
	}

	/**
	 * creates a new task from inputted information
	 * 
	 * @param name
	 * @param location
	 * @param description
	 * @param date
	 * @param time
	 * @param category
	 */
	private void newTask(String name, String locationName,String description,
			String date, String time, String category) {

		if (name.equals("")) {
			alertBox("No task name!", "Please give your task a name.");
		}
		
		//TODO copy this shit
		Date dueDate = null;
		Date dueTime = null;
		try {
			dueDate = (new SimpleDateFormat("MM/dd/yyyy")).parse(date);
			dueTime = (new SimpleDateFormat("hh:mm")).parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		taskBeingViewed.setName(name);
		taskBeingViewed.setDetailedDescription(description);
		taskBeingViewed.setDueDate(dueDate);
		taskBeingViewed.setDueTime(dueTime);
		taskBeingViewed.setComplete(false);
		currentUser.setLocations(locationSender.getLoc());
		if(locationSender.lastPick() != null) {
			OurLocation temp = new OurLocation(locationName,locationSender.lastPick());
			adapt.createLocation(temp, currentUser.getId());
			taskBeingViewed.setLocation(temp);
		}
		locationSender.setNewLast(null);
		if (category != null && !category.equals("")) {
			if (!currentUser.containsCategory(category)) {
				categories.add(category);
				adapter.notifyDataSetChanged();
			}
		}
		Category cat = currentUser.createCategory(category, currentColor, taskBeingViewed);
		newTaskDialog.dismiss();
		//adapt.editTask(taskBeingViewed);
		//adapt.editCategory(cat);
		viewRowsInTable();
	}

	/**
	 * handles clicks and calls the appropriate methods
	 * 
	 * @param view
	 */
	public void onClick(View view) {
		if (view.equals(addTask)) {
			taskBeingViewed = new Task();
			newTaskDialog();
		} else if (view.equals(allTasks)) {
			currentUser.setActiveCategory(null);
			dropDown.setSelection(0);
		}
		if (view instanceof CheckBox) {
			currentUser.switchTaskComplete(taskMap.get(view));
		}
		if (view instanceof TableRow) {
			int index = 0;
			Task task = (Task) ((TableRow) view).getTag();
			for (Task t : tasks) {
				if (t.equals(task)) {
					break;
				}
				index++;
			}
			taskBeingViewed = task;
			viewTaskDialog();
		}
	}

	/**
	 * Creates the row for a particular task that exists
	 * 
	 * @param t
	 *            the task in the row
	 */
	private void viewRowsInTable() {
		tableView.removeAllViews();
		View line = new View(activity);
		line.setBackgroundColor(Color.BLACK);
		line.setLayoutParams(new TableRow.LayoutParams(
				TableRow.LayoutParams.FILL_PARENT, 5));
		tableView.addView(line);
		tasks = new ArrayList<Task>();
		for (Task g : currentUser.getActiveCategory().getTasks()) {
			tasks.add(g);
			TableRow tr = new TableRow(activity);
			if(g.getCategory() != null) {
				if(g.getCategory().getColor() != 0) {
					tr.setBackgroundColor(g.getCategory().getColor());
				}
			}
			tr.setLayoutParams(new TableRow.LayoutParams(
					TableRow.LayoutParams.FILL_PARENT,
					TableRow.LayoutParams.WRAP_CONTENT));
			CheckBox cb = new CheckBox(activity);
			cb.setChecked(g.isComplete());
			cb.setLayoutParams(new TableRow.LayoutParams(100, 100));
			cb.setOnClickListener(this);
			taskMap.put(cb, g);
			TextView tv = new TextView(activity);
			tv.setLayoutParams(new TableRow.LayoutParams(400, 80));
			tv.setText(g.getName());
			tv.setTextSize(20);
			tv.setTextColor(Color.BLACK);
			tr.setTag(g);
			tr.addView(cb);
			tr.addView(tv);
			tr.setOnClickListener(this);
			tr.setClickable(true);
			tableView.addView(tr, new TableLayout.LayoutParams(
					TableRow.LayoutParams.FILL_PARENT,
					TableRow.LayoutParams.WRAP_CONTENT));

			View line2 = new View(activity);
			line2.setBackgroundColor(Color.BLACK);
			line2.setLayoutParams(new TableRow.LayoutParams(
					TableRow.LayoutParams.FILL_PARENT, 5));
			tableView.addView(line2);
		}

	}

	/**
	 * Finds all the task due after a certain date
	 * 
	 * @param date
	 *            the date that the user selects
	 */
	public void setAfterDate(String date) {
		Date dateTime = null;
		try {
			dateTime = (new SimpleDateFormat("MM/dd/yyyy")).parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		currentUser.tasksAfterDate(dateTime);
	}

	/**
	 * Lets program know which category is currently selected
	 * 
	 * @param parent
	 * @param view
	 * @param pos
	 * @param id
	 */
	public void onItemSelected(AdapterView<?> parent, View view, int pos,
			long id) {
		String cat = (String) parent.getItemAtPosition(pos);
		if (cat.equals("After date...")) {
			pickDateDialog(new AfterDatePicker());
		} else {
			currentUser.setActiveCategoryWithString((String) parent
					.getItemAtPosition(pos));
		}
		viewRowsInTable();
	}

	/**
	 * Lets program know nothing is selected
	 * 
	 * @param arg0
	 */
	public void onNothingSelected(AdapterView<?> arg0) {

	}


	/**
	 * 
	 * @author Tyler Bishop
	 * @version 1.0
	 */
	private class NewTask implements OurListener {
		public void onClick(View v) {
			if (v.equals(createTaskButton)) {
				newTask(((EditText) newTaskDialog.findViewById(R.id.nameField))
						.getText().toString(),
						((EditText) newTaskDialog.findViewById(R.id.locationField))
						.getText().toString(),
						((EditText) newTaskDialog.findViewById(R.id.desField))
								.getText().toString(),
						((EditText) newTaskDialog.findViewById(R.id.dateField))
								.getText().toString(),
						((EditText) newTaskDialog.findViewById(R.id.timeField))
								.getText().toString(),
						((AutoCompleteTextView) newTaskDialog
								.findViewById(R.id.category)).getText()
								.toString());
			} else if (v.equals(cancelButton)) {
				newTaskDialog.dismiss();
			} else if (v.equals(dateField)) {
				pickDateDialog(this);
			} else if (v.equals(timeField)) {
				pickTimeDialog(this);
			} else if(v.equals(locButton1)) {
				locationSender.setLocList(currentUser.getLocations());
				Intent myIntent = new Intent(activity.getBaseContext(), SelectMapActive.class);
	        	activity.startActivity(myIntent);
			} else if(v.equals(colorButton1)) {
				pickColorDialog(this);
			}
		}
		public OnClickListener newDate() {
			return new NewDate();
		}
		public OnClickListener newTime() {
			return new NewTime();
		}
		public OnClickListener newColor() {
			return new NewColor();
		}
		
		private class NewColor implements OnClickListener {
			public void onClick(View v) {
				for(ImageView b:colorButtons.keySet()) {
					if(v.equals(b)) {
						pickColorDialog.dismiss();
						colorButton1.setBackgroundResource(colorButtons.get(b));
						currentColor = buttonToColor.get(colorButtons.get(b));
						return;
					}
				}
			}
		}
		
		/**
		 * 
		 * @author Tyler Bishop
		 * @version 1.0
		 * 
		 */
		private class NewTime implements OnClickListener {
			public void onClick(View v) {
				if (v.equals(useTime)) {
					String time = ((timePicker.getCurrentHour() < 10) ? ("0" + timePicker
							.getCurrentHour()) : (timePicker.getCurrentHour()))
							+ ":"
							+ ((timePicker.getCurrentMinute() < 10) ? ("0" + timePicker
									.getCurrentMinute()) : (timePicker
									.getCurrentMinute()));
					((EditText) newTaskDialog.findViewById(R.id.timeField))
							.setText(time);
					pickTimeDialog.dismiss();
				} else if (v.equals(cancelTime)) {
					pickTimeDialog.dismiss();
				}
			}
		}

		/**
		 * 
		 * @author Tyler Bishop
		 * @version 1.0
		 * 
		 */
		private class NewDate implements OnClickListener {
			public void onClick(View v) {
				if (v.equals(useDate)) {

					String date = datePicker.getMonth() + 1 + "/"
							+ datePicker.getDayOfMonth() + "/"
							+ datePicker.getYear();
					((EditText) newTaskDialog.findViewById(R.id.dateField))
							.setText(date);
					pickDateDialog.dismiss();

				} else if (v.equals(cancelDate)) {
					pickDateDialog.dismiss();
				}
			}
		}
	}

	/**
	 * 
	 * @author Tyler Bishop
	 * @version 1.0
	 * 
	 */
	private class ViewTask implements OurListener {

		/**
		 * 
		 * @param v
		 */
		public void onClick(View v) {
			if (v.equals(editTaskButton)) {
				if (editTaskButton.getText().equals("Edit")) {
					inEdit = true;
					editTask();
				} else {
					inEdit = false;
					saveCurrentTask();
					viewTask();
				}
			}
			if (v.equals(doneButton)) {
				if (doneButton.getText().equals("Done")) {
					inEdit = false;
					viewTaskDialog.dismiss();
					viewRowsInTable();
				} else {
					cancelEditTask();
				}
			}
			if (inEdit) {
				if (v.equals(viewdateField)) {
					pickDateDialog(this);
				} else if (v.equals(viewtimeField)) {
					pickTimeDialog(this);
				} else if (v.equals(colorButton2)) {
					pickColorDialog(this);
				} else if (v.equals(locButton2)) {
					locationSender.setLocList(currentUser.getLocations());
					Intent myIntent = new Intent(activity.getBaseContext(), SelectMapActive.class);
		        	activity.startActivity(myIntent);
				}
			}
		}

		/**
		 * lets app know when a new date is being selected
		 * 
		 * @return the new date that was selected
		 */
		public OnClickListener newDate() {
			return new NewDate();
		}
		/**
		 * lets app know when a new time is being selected
		 * 
		 * @return
		 */
		public OnClickListener newTime() {
			return new NewTime();
		}
		public OnClickListener newColor() {
			return new NewColor();
		}
		
		private class NewColor implements OnClickListener {
			public void onClick(View v) {
				for(ImageView b:colorButtons.keySet()) {
					if(v.equals(b)) {
						pickColorDialog.dismiss();
						colorButton1.setBackgroundResource(colorButtons.get(b));
						currentColor = buttonToColor.get(colorButtons.get(b));
						return;
					}
				}
			}
		}
		/**
		 * 
		 * @author Tyler Bishop
		 * @version 1.0
		 */
		private class NewTime implements OnClickListener {
			public void onClick(View v) {
				if (v.equals(useTime)) {
					String time = (timePicker.getCurrentHour() < 10) ? ("0" + timePicker
							.getCurrentHour())
							: (timePicker.getCurrentHour())
									+ ":"
									+ ((timePicker.getCurrentMinute() < 10) ? ("0" + timePicker
											.getCurrentMinute()) : (timePicker
											.getCurrentMinute()));
					((EditText) viewTaskDialog.findViewById(R.id.viewTimeField))
							.setText(time);
					pickTimeDialog.dismiss();
				} else if (v.equals(cancelTime)) {
					pickTimeDialog.dismiss();
				}
			}
		}

		/**
		 * 
		 * @author Tyler Bishop
		 * @version 1.0
		 * 
		 */
		private class NewDate implements OnClickListener {
			public void onClick(View v) {
				if (v.equals(useDate)) {

					String date = datePicker.getMonth() + 1 + "/"
							+ datePicker.getDayOfMonth() + "/"
							+ datePicker.getYear();
					((EditText) viewTaskDialog.findViewById(R.id.viewDateField))
							.setText(date);
					pickDateDialog.dismiss();

				} else if (v.equals(cancelDate)) {
					pickDateDialog.dismiss();
				}
			}
		}

	}

	/**
	 * 
	 * @author Tyler Bishop
	 * @version 1.0
	 */
	private class AfterDatePicker implements OurListener {

		public void onClick(View arg0) {

		}

		public OnClickListener newTime() {
			return null;
		}

		public OnClickListener newDate() {
			return new NewDate();
		}
		public OnClickListener newColor() {
			return null;
		}
		
		/**
		 * 
		 * @author Tyler Bishop
		 * @version 1.0
		 */
		private class NewDate implements OnClickListener {
			public void onClick(View v) {
				if (v.equals(useDate)) {

					String date = datePicker.getMonth() + 1 + "/"
							+ datePicker.getDayOfMonth() + "/"
							+ datePicker.getYear();
					pickDateDialog.dismiss();
					setAfterDate(date);
					viewRowsInTable();
				} else if (v.equals(cancelDate)) {
					pickDateDialog.dismiss();
				}
			}
		}
	}

	/**
	 * 
	 * @author Tyler Bishop
	 * @version 1.0
	 */
	private interface OurListener extends OnClickListener {
		public OnClickListener newTime();
		public OnClickListener newDate();
		public OnClickListener newColor();
		
	}

}
